var minHeight = $(document.body).height() - $('.list').height() - $('.search').height() - $('#div1').height()
$('#localMap').height(minHeight)
//	搜索按钮
$('.but').click(function () {
    $('.search').css('background', 'url(img/search-2.png)');
    $('.search').css('background-size', '100% 100%');

})



$(function () {
    var Url = 'https://www.shandong10086.com/wechatflowfruit'
    // 百度地图API功能
    try {
        var map = new BMap.Map("localMap");
        // 创建Map实例

        map.centerAndZoom(new BMap.Point(117, 36.5), 18); // 初始化地�?设置中心点坐标和地图级别
        map.setCurrentCity("济南");          // 设置地图显示的城�?此项是必须设置的
        map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩�?
        //当前位置
        var latCurrent
        var lngCurrent
        var ipcityName
        var geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function (r) {
            if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                map.centerAndZoom(r.point, 16);
                var mk = new BMap.Marker(r.point);
                map.addOverlay(mk);
                map.panTo(r.point);
                latCurrent = r.point.lat;
                lngCurrent = r.point.lng;
                // alert('您的位置�?+r.point.lng+','+r.point.lat);
            }
            else {
                alert('当前位置获取失败');
            }
            //ip
            function myFun(result) {
                var ipcityName = result.name;
            }
            var myCity = new BMap.LocalCity();
            myCity.get(myFun);
        }, { enableHighAccuracy: true })
        //查位�?
        $('.but').on('click', function () {
            var bname = $('#txt').val()
            if (bname) {
                eachSearch(bname, ipcityName)
            } else {
                alert('请输入地址')
            }
        })
        // //搜索框模糊查�?
        document.getElementById("txt").addEventListener("input", inputSearch, false);
        function inputSearch() {
            var bname = $('#txt').val()
            if (bname) {
                eachSearch(bname, ipcityName)
            }
        }
        //�?
        $.post(Url + '/yyd/city', function (res) {
            //console.log(res)
            if (res.success) {
                //初始化市下拉列表
                for (var i = 0; i < res.data.length; i++) {
                    $("#province").append($("<option></option>").val(res.data[i].code).html(res.data[i].city));
                }
            }
        })

        //为省份下拉列表绑定change事件
        $("#province").change(function () {
            $("#city").prop("length", 1);//清空原有的数�?
            var cityCode = $('#province option:selected').val()
            $.post(Url + '/yyd/area', { parentCode: cityCode }, function (res) {
                // console.log(res)
                if (res.success) {
                    //初始化区下拉列表
                    for (var i = 0; i < res.data.length; i++) {//重新为区赋�?
                        $("#city").append($("<option>").val(res.data[i].code).html(res.data[i].district));
                    }
                }
            })
        });
        $('#city').change(function () {
            var areaCode = $('#city option:selected').val()
            var cityName = $('#province option:selected').text()
            eachSearch(areaCode, cityName)
        })
        //遍历营业点查�?
        function eachSearch(param, cityName) {
            var params
            if (/^[0-9]+.?[0-9]*/.test(param)) {
                params = 'areaCode'
            } else {
                params = 'bname'
            }
            var data = '{"' + params + '":"' + param + '"}'
            $.post(Url + '/yyd/getYingYeTing', JSON.parse(data), function (res) {
                console.log(res)
                if (res.success) {
                    $('.list').empty()
                    $.each(res.data, function (i, e) {
                        var str = '<li><div style="float: left;" class="adress" data-localX="' + e.localX + '" data-localY="' + e.localY + '">' +
                            '<p class="name">' + e.businessHallName + '</p>' +
                            '<p class="position"><i></i>' + e.businessHallAddress + '</p></div>' +
                            '<a class="phone" href="tel:' + e.phone + '"><img src="img/phone.png"/></a></li>'
                        $('.list').append(str)
                    });
                }
            }).done(function () {
                //点击地址
                $('.adress').on('click', function () {
                    var localX = $(this).attr('data-localX')
                    var localY = $(this).attr('data-localY')
                    console.log(localX, localY)
                    if (localX) {
                        var point = new BMap.Point(localY, localX);
                        map.centerAndZoom(point, 18);
                        var marker = new BMap.Marker(point);  // 创建标注
                        map.addOverlay(marker);              // 将标注添加到地图�?
                        map.panTo(new BMap.Point(localY, localX))
                        marker.addEventListener("click", getAttr);
                        //监听点击事件
                        function getAttr() {
                            var p = marker.getPosition();       //获取marker的位�?
                            console.log(p)
                            // alert("marker的位置是" + p.lng + "," + p.lat);
                            // alert(lngCurrent)
                            // alert(latCurrent)
                            location.href = "http://api.map.baidu.com/direction?origin=" + lngCurrent + "," + latCurrent + "&destination=" + p.lat + "," + p.lng + "&mode=driving&region=济南&output=html";
                        }
                    } else {
                        var name = $($(this).children().eq(0)).text()
                        console.log(name, cityName)
                        enterAdress(name, cityName)
                    }
                })
            })
        }
        //传入地址
        function enterAdress(adress, city) {
            // 创建地址解析器实�?
            var myGeo = new BMap.Geocoder();
            console.log(adress, city)
            myGeo.getPoint(adress, function (point) {
                console.log(point)
                if (point) {
                    map.centerAndZoom(point, 18);
                    var marker = new BMap.Marker(point);  // 创建标注
                    map.addOverlay(marker);               // 将标注添加到地图�?
                    marker.addEventListener("click", getAttr);
                    //监听点击事件
                    function getAttr() {
                        var p = marker.getPosition();       //获取marker的位�?
                        console.log(p)
                        // alert("marker的位置是" + p.lng + "," + p.lat);
                        // alert(lngCurrent)
                        // alert(latCurrent)
                        location.href = "http://api.map.baidu.com/direction?origin=" + lngCurrent + "," + latCurrent + "&destination=" + p.lat + "," + p.lng + "&mode=driving&region=济南&output=html";
                    }
                } else {
                    alert('没有找到相关位置')
                }
            }, city);
        }
    } catch (error) {
        window.location.reload()
    }

})


